<?php 
/**
 * This file is part of DomFg.
 * 
 * DomFg is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * DomFg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with DomFg.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @link http://inditel.ee/
 * @copyright 2007-2009 Inditel Meedia OÜ
 * @author Oliver Leisalu <oliver@inditel.ee>
 */

/**
* @author Oliver Leisalu, Inditel Meedia
* @package DomFg
* @subpackage Panel
*/
class DomFgTabPanel extends DomFgPanel {
	
	protected $defaultTab = 0;
	
	protected $tabsStyle;
	/**
	* 
	* @var Bool
	*/
	protected $headerTabs = false;
	
	const SESSION_VAR = 'domfgtabpanel';
	
	public function defaultTab( $name ) {
		$this->defaultTab = $name;
		return $this;
	}
	
	public function headerTabs( $headerTabs ) {
		$this->headerTabs = (bool)$headerTabs;
		return $this;
	}
	
	public function tabsStyle( $tabsStyle ) {
		$this->tabsStyle = $tabsStyle;
		return $this;
	}
	
	public function addItem( DomFgComponent $item ) {
		return $this->items[] = $item;
	}
	
	public static function getDefaultChild() {
		return 'panel';
	}
	
	public function render() {
		
		$tabs = array();
		$contents = '';
		$groupId = 'tabGroup_'.($this->id ? $this->id : ($this->name ? $this->name : md5($this->title)));
				
		// selecting tab from which content was sent.
		if( isset($_POST[$groupId.'_selectedTab']) ) 
			$selectedTab = $_POST[$groupId.'_selectedTab'];
			
		// selecting tab from URL line.
		if( isset($_GET[$this->id.'tab']) ) 
			$selectedTab = $_GET[$this->id.'tab'];
		
		if( $selectedTab === NULL )
			$selectedTab = $this->defaultTab;	

		$tabNr = 0;
		foreach( $this->items as $i => $item ) {

			if( $item->isTab == true or ($item->isTab === NULL && $item instanceof DomFgPanel ) ) {
				$tabId = 'tab_'.($item->id ? $item->id : ($item->name ? $item->name : md5($item->title)));
				
				// if tab is selected by tab name then change it to tabNr.
				if( !is_numeric($selectedTab) && $tabId == 'tab_'.$selectedTab or is_numeric($selectedTab) && $tabNr == $selectedTab ) {
					$selectedTab = $tabNr;
					$selected = true;
				} else {
					$selected = false;
				}
				//debug($tabNr.' == '.$selectedTab.' = '.($tabNr == $selectedTab).' isnumeric: '.is_numeric($selectedTab));	
				
				$tabs[] = array(
									'groupId' => $groupId, 
									'id' => $tabId, 
									'title' => $item->title,
									'nr' => $tabNr,
									'selected' => $selected,
								);
				
				$contents .= '<div id="'.$tabId.'_frame" style="'.($selected ? NULL : 'display:none').'">' . $item->render() . '</div>';
				$tabNr++;
			} else {
				$contents .= $item->render();
			}
		}
		
		$buttons = '';
		foreach( $this->buttons as $button ) 
			$buttons .= $button->render();
		
		
		
		$r = new DomSmarty();
		$r->template_dir = DomFg::getTemplatePath();
		$r->assign('templateUrl', DomFg::getTemplateUrl() );
		$r->assign('panel', $this );
		$r->assign('selectedTab', $selectedTab );
		$r->assign('tabs', $tabs );
		$r->assign('groupId', $groupId );
		$r->assign('tabs', $r->fetch( $this->headerTabs ? 'tabs.html' : 'innerTabs.html') );
		
		$r->assign('contents', $contents );
		$r->assign('buttons', $buttons );
		$r->assign('fields', DomFgPost::getInstance()->getFields() );
		
		return $r->fetch('panel.html');
	}
	

}
?>